﻿using CodeCube.Common;
using CodeCube.Core;
using CodeCube.Service.DtoModel;
using CodeCube.Service.Interfaces;
using CodeCube.Service.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CodeCube.Service.Implements
{
    public class LoginOperateService : DbContext, ILoginOperateService
    {
        IUserRolesService _ur;
        /// <summary>
        /// 初始化注入
        /// </summary>
        /// <param name="ur"></param>
        public LoginOperateService(IUserRolesService ur)
        {
            _ur = ur;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public async Task<ApiResult<UserAccountModel>> LoginAsync(string Account, string Password)
        {
            var res = new ApiResult<UserAccountModel>();
            var mdl = await Db.Queryable<UserAccountModel>().Where(m => m.Account == Account).SingleAsync();
            if (mdl != null)
            {
                if (Md5Crypt.Encrypt(Password) == mdl.Password)
                {
                    res.success = true;
                    res.message = "登录成功！";
                    res.data = mdl;
                }
                else
                {
                    res.success = false;
                    res.message = "密码错误！";
                }
            }
            else
            {
                res.success = false;
                res.message = "账号不存在！";
            }
            return res;
        }
        /// <summary>
        /// 获取用户权限范围内的菜单项列表(不分层)
        /// </summary>
        /// <param name="UserGuid"></param>
        /// <returns></returns>
        public async Task<List<string>> GetMenuItemListAsync(string UserGuid)
        {
            var rolelist = await Db.Queryable<UserRolesModel>().Where(m => m.UserGuid == UserGuid).ToListAsync();
            var rlist = new List<string>();
            foreach(var s in rolelist)
            {
                rlist.Add(s.RoleGuid);
            }
            var menulist = await Db.Queryable<SysRoledataModel>().Where(m => rlist.Contains(m.RoleGuid)).ToListAsync();
            var mlist = new List<string>();
            foreach(var m in menulist)
            {
                mlist.Add(m.MenuGuid);
            }
            return mlist;
        }
        private async Task<List<SysMenuModel>> GetMenuList(string ParentGuid, List<string> MenuList)
        {
            var result = await Db.Queryable<SysMenuModel>().Where(m => m.ParentGuid == ParentGuid && MenuList.Contains(m.Guid)).ToListAsync();
            return result;
        }
        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <param name="ParentGuid"></param>
        /// <param name="MenuList"></param>
        /// <returns></returns>
        public async Task<List<SysMenuDto>> GetMenuTreeAsync(string ParentGuid,List<string> MenuList)
        {
            var result = new List<SysMenuDto>();
            var tmp = await GetMenuList(ParentGuid, MenuList);
            if (tmp.Count > 0)
            {
                foreach (var m in tmp)
                {
                    var t = (SysMenuDto)m;
                    t.Child = await GetMenuTreeAsync(t.Guid, MenuList);
                    result.Add(t);
                }
            }
            return result;
        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<string>> LogoutAsync()
        {
            throw new NotImplementedException();
        }
    }
}
